﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyLibary;
using ReadConfig;
using System.IO;

namespace FinGu.Testing.ApplicationLayout
{
    public class MyApplication
    {
        private string ServerAddress;
        private string UserName;
        private string PassWord;
        public void SetServerAddress(string arg)
        {
            ServerAddress = arg;
        }
        public void SetUserName(string arg)
        {
            UserName = arg;
        }
        public void SetPassWord(string arg)
        {
            PassWord = arg;
        }
        public string GetServerAddress()
        {
            return ServerAddress;
        }
        public string GetUserName()
        {
            return UserName;
        }
        public string GetPassWord()
        {
            return PassWord;
        }
        public struct Shop_Server
        {
            public static string BigShop = "192.168.23.254";
            public static string SmallShop = "192.168.2.252";
            public static string UserName = "update";
            public static string PassWord = "update1512";
        }

        private string ConfigBoardAddress;
        private string ConfigBoardUserName;
        private string ConfigBoardPassWord;
        public void SetConfigBoardAddress(string arg)
        {
            ConfigBoardAddress = arg;
        }
        public void SetConfigBoardUserName(string arg)
        {
            ConfigBoardUserName = arg;
        }
        public void SetConfigBoardPassWord(string arg)
        {
            ConfigBoardPassWord = arg;
        }
        public string GetConfigBoardAddress()
        {
            return ConfigBoardAddress;
        }
        public string GetConfigBoardUserName()
        {
            return ConfigBoardUserName;
        }
        public string GetConfigBoardPassWord()
        {
            return ConfigBoardPassWord;
        }
        public struct ConfigBoard_Server
        {
            public static string Address = "http://192.168.36.253:2801/books/自动化平台/通用自动测试配置文件20121012.xls";
            public static string UserName = "dvc";
            public static string PassWord = "shanshan";
        }

        static string ManagerPassWord = "xjhq123123";
        public static string GetManagerPassWord()
        {
            return ManagerPassWord;
        }

        /*
        const string _SWName = "自动化平台_文件配置工具";
        const string _SWVersion = "P1.0";
        const string _SWDescription = "修改了软件更新部分：xxxxxxxxxxxxx。";
        const string _SWVersionCheckTable = "[mcsdbtrial].[dbo].[Platform_UpdateSoftwave]";
        const string _SWUpdateProgramName = "SoftWaveUpdateList.exe";
        const string _SWUpdateProgramPath = "update\\";
        public string GetSWName()
        {
            return _SWName;
        }
        public string GetSWVersion()
        {
            return _SWVersion;
        }
        public string GetSWDescription()
        {
            return _SWDescription;
        }
        public string GetSWVersionCheckTable()
        {
            return _SWVersionCheckTable;
        }
        public string GetSWUpdateProgramName()
        {
            return _SWUpdateProgramName;
        }
        public string GetSWUpdateProgramPath()
        {
            return _SWUpdateProgramPath;
        }
        */

        private StatusStrip _statusStrip;
        public void SetStatusStrip(StatusStrip statusStrip)
        {
            try
            {
                this._statusStrip = statusStrip;
            }
            catch (NullReferenceException ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
        public StatusStrip GetStatusStrip()
        {
            return this._statusStrip;
        }

        private ComboBox _cobProductContent;
        public void SetComboBox(ComboBox cob)
        {
            try
            {
                this._cobProductContent = cob;
            }
            catch (NullReferenceException ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public ComboBox GetCombox()
        {
            return this._cobProductContent;
        }

        private AxDSOFramer.AxFramerControl _axFramerControl1;
        public void SetAxFramerControl(AxDSOFramer.AxFramerControl axFramerControl1)
        {
            try
            {
                this._axFramerControl1 = axFramerControl1;
            }
            catch (NullReferenceException ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public AxDSOFramer.AxFramerControl GetAxFramerControl()
        {
            return this._axFramerControl1;
        }

        Ftpable ftp;
        Export exp;
        ExcelLibary excellib = new ExcelLibary();
        SqlLibary sqllib = new SqlLibary();

        public MyApplication() { }
        ~MyApplication() { }
        public MyApplication(StatusStrip statusStrip, ComboBox cob, AxDSOFramer.AxFramerControl axFramerControl1)
        {
            try
            {
                this._statusStrip = statusStrip;
                this._cobProductContent = cob;
                this._axFramerControl1 = axFramerControl1;
            }
            catch (NullReferenceException ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// 转化工序名称描述
        /// </summary>
        /// <param name="arg">转化前的工序名称</param>
        /// <returns>转化后的工序名称</returns>
        public string TransformProcessNameDescription(string arg)
        {
            string result = "";

            if (arg.Contains("常规"))
            {
                result = "常规测试";
            }

            if (arg.Contains("互调"))
            {
                result = "互调测试";
            }

            if (arg.Contains("峰值"))
            {
                result = "峰值功率测试";
            }

            if (arg.Contains("平均"))
            {
                result = "平均功率测试";
            }

            if (arg.Contains("噪声"))
            {
                result = "华为噪声测试";
            }

            if (arg.Contains("噪声"))
            {
                result = "爱立信噪声测试";
            }

            if (arg.Contains("1dB"))
            {
                result = "1dB压缩点";
            }

            if (arg.Contains("IIP3"))
            {
                result = "华为IIP3";
            }

            if (arg.Contains("OIP3"))
            {
                result = "塔放OIP3";
            }

            if (arg.Contains("驻波"))
            {
                result = "阿朗驻波";
            }

            if (arg.Contains("EVM"))
            {
                result = "EVM";
            }

            return result;
        }
        /// <summary>
        /// 在状态脚本上显示消息
        /// </summary>
        /// <param name="arg">需要显示的消息</param>
        public void DisplayMessageToStatusStrip(string arg)
        {
            GetStatusStrip().Items[0].Text = arg;
        }
        /// <summary>
        /// 将服务器上的config文件列表放入窗口列表中。
        /// </summary>
        public bool LoadConfigFileToList(ref string msg)
        {
            #region 设置配置文件模板的服务器登录信息

            SetConfigBoardAddress(ConfigBoard_Server.Address);
            SetConfigBoardUserName(ConfigBoard_Server.UserName);
            SetConfigBoardPassWord(ConfigBoard_Server.PassWord);

            #endregion

            //---------------------------------------------------
            //将服务器上的config文件列表放入窗口列表中。
            //---------------------------------------------------   
            if (GetConfigBoardAddress() != ""
                && GetConfigBoardUserName() != ""
                && GetConfigBoardPassWord() != "")
            {
                DisplayMessageToStatusStrip("close local pc's proxy.");

                ftp = new Ftpable();

                ftp.FtpUpDown(GetServerAddress(), GetUserName(), GetPassWord());

                string[] configFiles = ftp.GetFileList();

                GetCombox().Items.Clear();

                foreach (string arg in configFiles)
                {
                    GetCombox().Items.Add(arg);
                }

                //---------------------------------------------------
                //将添加的内容作为数据源放入list中。
                GetCombox().AutoCompleteSource = AutoCompleteSource.ListItems;
                //按照用户输入的信息来显示list中的内容。
                GetCombox().AutoCompleteMode = AutoCompleteMode.SuggestAppend;

                DisplayMessageToStatusStrip("load special ftp server's lists of config files.");

                return true;
            }
            else
            {
                msg = "在获取ftp服务器中config文件失败！请确认ftp服务器登录信息完整，并正确！";
                return false;
            }

        }
        /// <summary>
        /// 从指定服务器上下载自动化测试配置模板,并另存为alias001.xls。
        /// </summary>
        public void DownLoadConfigBoard()
        {
            //---------------------------------------------------
            //从指定服务器上下载自动化测试配置模板
            //---------------------------------------------------
            GetAxFramerControl().Dock = DockStyle.Fill;
            string _sFilePath = GetConfigBoardAddress();
            string _sFileType = "Excel.Sheet";
            Boolean _bOnlyRead = true;
            string _sUserName = GetConfigBoardUserName();
            string _sPassWord = GetConfigBoardPassWord();
            GetAxFramerControl().Open(_sFilePath, _bOnlyRead, _sFileType, _sUserName, _sPassWord);
            GetAxFramerControl().Save(Directory.GetCurrentDirectory() + "\\WorkArea\\alias001.xls", true, "", "");
            GetAxFramerControl().Close();
            DisplayMessageToStatusStrip("down load testing config board from special server.");
        }
        /// <summary>
        /// 从指定FTP上下载指定 产品、工序、版本的测试模板
        /// </summary>
        public void DownLoadSpecialConfigFile()
        {
            //---------------------------------------------------
            //从指定FTP上下载指定 产品、工序、版本的测试模板
            //---------------------------------------------------
            string errorinfo;
            ftp.Download(GetServerAddress(), Directory.GetCurrentDirectory() + "\\WorkArea\\", GetCombox().Text, out errorinfo);
            DisplayMessageToStatusStrip("down load testing config file from ftp server.");
        }
        /// <summary>
        /// 将产品的测试配置文件内容拷贝到测试配置文件模板中
        /// </summary>
        public void ConfigFileCopyToConfigBoard()
        {
            //---------------------------------------------------
            //将产品的测试配置文件内容拷贝到测试配置文件模板中
            //---------------------------------------------------
            string _sSource = Directory.GetCurrentDirectory() + "\\WorkArea\\" + GetCombox().Text;
            string _sDestination = Directory.GetCurrentDirectory() + "\\WorkArea\\alias001.xls";
            string[] tmp = GetCombox().Text.Split('_');
            string _sProcessName = TransformProcessNameDescription(tmp[1]);

            exp = new ReadConfig.Export(_sSource, _sDestination, _sProcessName);

            exp.OutPutData();

            excellib.KillExcel();

            GetAxFramerControl().Open(_sDestination);

            DisplayMessageToStatusStrip("put testing config file's content copy to testing excel file.");
        }
        public bool CreateDirection(string path, ref string msg)
        {
            try
            {
                DirectoryInfo dirInfo = new DirectoryInfo(path);

                if (!dirInfo.Exists)
                {
                    dirInfo.Create();
                }

                msg = path + "创建成功！";

                return true;
            }
            catch (System.Exception ex)
            {
                msg = path + "创建失败！详细：" + ex.Message;

                return false;
            }

        }
        public bool InitFtpServerInfo(string address, string username, string password, ref string msg)
        {
            if (address != "" && username != "" && password != "")
            {
                SetServerAddress(address);
                SetUserName(username);
                SetPassWord(password);
                msg = "FTP服务器初始化失败！请确认服务器地址，用户登录账号是否为空，并正确。";
                return true;
            }
            else
            {
                msg = "FTP服务器初始化成功！";
                return true;
            }
        }

    }
}
